﻿using Constants = LightCAD.Runtime.Constants;

namespace QdArch
{
    public partial class WallAction : ElementAction
    {

        public WallAction() { }
        public WallAction(IDocumentEditor docEditor) : base(docEditor)
        {
            this.commandCtrl.WriteInfo("命令：Wall");
            //ElementActions.w = new LineAction();
            //LcDocument.ElementActions.Add(BuiltinElementType.Line, ElementActions.Line);
        }


        private static readonly LcCreateMethod[] CreateMethods;
        private List<Vector2> points;

        private Vector2 firstPoint { get; set; }

        private Vector2 secondPoint { get; set; }

        private PointInputer inputer { get; set; }
        static WallAction()
        {
            CreateMethods = new LcCreateMethod[1];
            CreateMethods[0] = new LcCreateMethod()
            {
                Name = "CreateWall",
                Description = "创建墙",
                Steps = new LcCreateStep[]
                {
                    new LcCreateStep { Name = "Step0", Options = "指定第一个点:" },
                    new LcCreateStep { Name = "Step1", Options = "直墙下一点或 [弧墙(A)/矩形画墙(R)/闭合(C)/回退(U)]<另一段>:" },
                    new LcCreateStep { Name = "Step2", Options = "指定下一个点或[放弃(U)]:" },
                }
            };
        }


        internal static void Initilize()
        {
            ArchActions.WallAction = new WallAction();
            LcDocument.ElementActions.Add(ArchElementType.Wall, ArchActions.WallAction);
        }

        public async void ExecCreate(string[] args = null)
        {
            //DocumentManager.CurrentRecorder.BeginAction("Wall");

            this.points = new List<Vector2>();
            this.StartCreating();
            this.inputer = new PointInputer(this.docEditor);
            var curMethod = CreateMethods[0];

            Step0:
            var step0 = curMethod.Steps[0];
            var result0 = await inputer.Execute(step0.Options);
            //var result0 = await inputer.Execute(step0.Options);

            if (inputer.isCancelled)
            {
                this.Cancel();
                goto End;
            }

            // zcb: 增加Res0为空的判定
            if (result0 == null)
            {
                this.Cancel();
                goto End;
            }

            if (result0.ValueX == null)
            {
                if (result0.Option != null)
                {
                    goto Step0;
                    //TODO:AutoCAD画线输入一个数字，是怎么确定点的？
                }
                else
                {
                    goto Step0;
                }
            }

            this.firstPoint = (Vector2)result0.ValueX;

            Step1:
            var step1 = curMethod.Steps[1];
            if (this.points.Count >= 2)
            {
                step1 = curMethod.Steps[2];
            }

            var result1 = await inputer.Execute(step1.Options);
            if (inputer.isCancelled)
            {
                this.Cancel();
                goto End;
            }

     

            if (result1.Option != null)
            {
                if (result1.Option == "U") { }
                else if (result1.Option == "C")
                {
                    goto End;
                }
                else if (result1.Option == " ")
                {
                    goto End;
                }
                else if (result1.ValueX != null)
                {
                    Vector2 pointB = (Vector2)result1.ValueX;
                    Vector2 direction = (pointB - firstPoint).Normalize();
                    result1.ValueX = firstPoint + direction * double.Parse(result1.Option);
                    //TODO:AutoCAD画线输入一个数字，是怎么确定点的？
                }
                else
                {
                    goto Step1;
                }
            }

            this.secondPoint = (Vector2)result1.ValueX;


            CreateWall(firstPoint, secondPoint);
            firstPoint = secondPoint;
            goto Step1;

            End:
            this.inputer = null;
            this.EndCreating();

            //if (this.points.Count > 0)
            //{
            //    DocumentManager.CurrentRecorder.EndAction();
            //}
            //else
            //{
            //    DocumentManager.CurrentRecorder.CancleAction();
            //}
        }
        public override void Draw(SKCanvas canvas, LcElement element, Vector2 offset)
        {
            Matrix3 matrix = Matrix3.GetTranslate(offset);
            var wall = element as QdWall;
            var mstart = matrix.MultiplyPoint(wall.BaseLine.Start);
            var mend = matrix.MultiplyPoint(wall.BaseLine.End);
            var start = this.vportRt.ConvertWcsToScr(mstart).ToSKPoint();
            var end = this.vportRt.ConvertWcsToScr(mend).ToSKPoint();

            using (var elePen = new SKPaint { Color = SKColors.Gray, IsStroke = true, PathEffect = SKPathEffect.CreateDash(new float[] { 10, 10, 10, 10 }, 0) })
            {
                canvas.DrawLine(start, end, elePen);
            }
            foreach (var item in wall.Curves)
            {
                Line2d line2D = item as Line2d;
                mstart = matrix.MultiplyPoint(line2D.Start);
                mend = matrix.MultiplyPoint(line2D.End);
                start = this.vportRt.ConvertWcsToScr(mstart).ToSKPoint();
                end = this.vportRt.ConvertWcsToScr(mend).ToSKPoint();
                var pen = this.GetDrawPen(element);
                if (pen == Constants.defaultPen)
                {
                    using (var elePen = new SKPaint { Color = new SKColor(element.GetColorValue()), IsStroke = true })
                    {
                        canvas.DrawLine(start, end, elePen);
                    }
                }
                else
                {
                    canvas.DrawLine(start, end, pen);
                }
            }


        }
        public override ControlGrip[] GetControlGrips(LcElement element)
        {
            var group = element as QdWall;
            var grips = new List<ControlGrip>();
            var gripCenter = new ControlGrip
            {
                Element = group,
                Name = "Center",
                Position = group.BoundingBox.Center,

            };
            grips.Add(gripCenter);

            var gripStart = new ControlGrip
            {
                Element = group,
                Name = "Start",
                Position = group.BaseLine.Start
            };
            grips.Add(gripStart);
            var gripEnd = new ControlGrip
            {
                Element = group,
                Name = "End",
                Position = group.BaseLine.End
            };
            grips.Add(gripEnd);



            return grips.ToArray();
        }
        private string _gripName;
        private Vector2 _position;
        private Vector2 _endDrag;
        private QdWall qdWall;
        private List<QdWall> fWalls;
        public override void DrawDragGrip(SKCanvas canvas)
        {

            if (qdWall == null) return;
            if (_gripName == "Center")
            {
                Matrix3 matrix3d = Matrix3.GetMove(qdWall.BoundingBox.Center, _position);
                Line2d line = new Line2d();
                line.Start = matrix3d.MultiplyPoint(qdWall.BaseLine.Start);
                line.End = matrix3d.MultiplyPoint(qdWall.BaseLine.End);
                Draw(canvas, line);
            }
            else
            {
                Line2d line = new Line2d();
                fWalls = new List<QdWall>();
                line = new Line2d();
                Vector2 po;
                if (_gripName == "Start")
                {
                    line.Start = _position;
                    line.End = qdWall.BaseLine.End;
                    po = qdWall.BaseLine.Start;
                }
                else
                {
                    line.Start = qdWall.BaseLine.Start;
                    line.End = _position;
                    po = qdWall.BaseLine.End;
                }
                Draw(canvas, line);
                foreach (ControlGrip item in this.vportRt.ElementGrips)
                {
                    if (item.Position.Similarity(po))
                    {
                        QdWall w = item.Element as QdWall;
                        if (w != qdWall)
                        {

                            if (item.Name == "Start")
                            {
                                line = new Line2d();
                                line.Start = _position;
                                line.End = w.BaseLine.End;
                                Draw(canvas, line);
                            }
                            else
                                if (item.Name == "End")
                            {
                                line = new Line2d();
                                line.Start = w.BaseLine.Start;
                                line.End = _position;
                                Draw(canvas, line);
                            }
                        }
                    }
                }

            }



        }
        private void RestWalls(QdWall wall)
        {
            List<QdWall> Crosswalls = GetCrossWalls(wall.BaseLine);
            if (Crosswalls.Count > 0)
            {
                foreach (QdWall item in Crosswalls)
                {
                    if (item.BaseLine.Start.Similarity(wall.BaseLine.Start) || item.BaseLine.Start.Similarity(wall.BaseLine.End) || item.BaseLine.End.Similarity(wall.BaseLine.Start) || item.BaseLine.End.Similarity(wall.BaseLine.End))
                    {
                        CornerWalls(wall, item);
                    }
                    else
                    {
                        Vector2 cp = Line2d.GetCrossVector(wall.BaseLine, item.BaseLine);
                        double sc = Vector2.Distance(cp, wall.BaseLine.Start);
                        double ec = Vector2.Distance(cp, wall.BaseLine.End);
                        double cl = sc < ec ? sc : ec;
                        if (cl <= (item.Width / 2))//交点小于墙宽就算 转角墙
                        {
                            if (sc < ec)
                            {

                                ConnectWall(wall, item, wall.BaseLine.Start);
                            }
                            else
                            {
                                ConnectWall(wall, item, wall.BaseLine.End);

                            }
                        }
                        else//交叉墙 需要打断
                        {
                            CrossWall(wall, item);
                        }
                    }
                }

            }
            else
            {

            }
        }
        public override void SetDragGrip(LcElement element, string gripName, Vector2 position, bool isEnd)
        {
            qdWall = element as QdWall;

            if (!isEnd)
            {
                _gripName = gripName;
                _position = position;
            }
            else
            {

                if (gripName == "Center")
                {
                    List<QdWall> Crosswalls = GetCrossWalls(qdWall.BaseLine);
                    Matrix3 matrix3d = Matrix3.GetMove(qdWall.BoundingBox.Center, _position);
                    Line2d line = new Line2d();
                    line.Start = matrix3d.MultiplyPoint(qdWall.BaseLine.Start);
                    line.End = matrix3d.MultiplyPoint(qdWall.BaseLine.End);
                    qdWall.BaseCurve = line;
                    qdWall.CreateShapeCurves();
                    foreach (var item in GetCrossWalls(line))
                    {
                        if (!Crosswalls.Contains(item))
                        {
                            Crosswalls.Add(item);
                        }
                    }
                    foreach (QdWall item in Crosswalls)
                    {
                        item.CreateShapeCurves();

                    }
                    foreach (QdWall item in Crosswalls)
                    {
                        DisposeWalls(item, false);
                       // RestWalls(item);
                    }
                    DisposeWalls(qdWall, false);


                    var args = new PropertyChangedEventArgs
                    {
                        Object = qdWall,
                        PropertyName = "",
                        OldValue = null,
                        NewValue = null,
                    };
                   // this.PropertyChangedAfter?.Invoke(this, args);
                   this.docRt.Document.OnPropertyChangedAfter(args);
                    //RestWalls(qdWall);
                }
                else
                {
                    List<QdWall> Crosswalls = GetCrossWalls(qdWall.BaseLine);
                    List<QdWall> changewalls = new List<QdWall>();
                    Line2d line = new Line2d();
                    fWalls = new List<QdWall>();
                    line = new Line2d();
                    Vector2 po;
                    if (_gripName == "Start")
                    {
                        line.Start = _position;
                        line.End = qdWall.BaseLine.End;
                        po = qdWall.BaseLine.Start;
                        qdWall.BaseCurve = line;
                    }
                    else
                    {
                        line.Start = qdWall.BaseLine.Start;
                        line.End = _position;
                        po = qdWall.BaseLine.End;
                        qdWall.BaseCurve = line;
                    }
                    changewalls.Add(qdWall);
                    foreach (var item in GetCrossWalls(line))
                    {
                        if (!Crosswalls.Contains(item))
                        {
                            Crosswalls.Add(item);
                        }
                    }
                    foreach (ControlGrip item in this.vportRt.ElementGrips)
                    {
                        if (item.Position.Similarity(po))
                        {
                            QdWall w = item.Element as QdWall;
                            if (w != qdWall)
                            {
                                if (item.Name == "Start")
                                {
                                    Crosswalls.AddRange(GetCrossWalls(qdWall.BaseLine));
                                    line = new Line2d();
                                    line.Start = _position;
                                    line.End = w.BaseLine.End;
                                    w.BaseCurve = line;
                                    changewalls.Add(w);
                                }
                                else
                                    if (item.Name == "End")
                                {
                                    Crosswalls.AddRange(GetCrossWalls(qdWall.BaseLine));
                                    line = new Line2d();
                                    line.Start = w.BaseLine.Start;
                                    line.End = _position;
                                    w.BaseCurve = line;
                                    changewalls.Add(w);
                                }
                            }
                        }
                    }
                    foreach (QdWall item in Crosswalls)
                    {
                        item.CreateShapeCurves();
                    }
                    foreach (QdWall item in changewalls)
                    {
                        item.CreateShapeCurves();
                    }
                    foreach (QdWall item in Crosswalls)
                    {
                        DisposeWalls(item, false);
                    }
                    foreach (QdWall item in changewalls)
                    {
                        DisposeWalls(item, false);
                        var args = new PropertyChangedEventArgs
                        {
                            Object = item,
                            PropertyName = "",
                            OldValue = null,
                            NewValue = null,
                        };
                        this.docRt.Document.OnPropertyChangedAfter(args);
                    }
                }
            }
        }
        public void Draw(SKCanvas canvas, Line2d line)
        {


            var sk_p = this.vportRt.ConvertWcsToScr(line.End).ToSKPoint();
            var start = this.vportRt.ConvertWcsToScr(line.Start).ToSKPoint();
            using (var elePen = new SKPaint { Color = SKColors.Yellow, IsStroke = true, PathEffect = SKPathEffect.CreateDash(new float[] { 10, 10, 30, 10 }, 0) })
            {
                canvas.DrawLine(start, sk_p, elePen);
            }

            foreach (var item in QdWall.GetShapeCurves(line, 5))
            {
                Line2d line2D = item as Line2d;
                //mstart = matrix.MultiplyPoint(line2D.Start);
                //mend = matrix.MultiplyPoint(line2D.End);
                start = this.vportRt.ConvertWcsToScr(line2D.Start).ToSKPoint();
                var end = this.vportRt.ConvertWcsToScr(line2D.End).ToSKPoint();
                using (var elePen = new SKPaint { Color = SKColors.Gray, IsStroke = true })
                {
                    canvas.DrawLine(start, end, elePen);
                }

            }
        }
        public override void DrawTemp(SKCanvas canvas)
        {
            if (this.firstPoint == null)
            {
                return;
            }

            var mp = this.vportRt.PointerMovedPosition.ToVector2d();
            var wcs_mp = this.vportRt.ConvertScrToWcs(mp);
            var sk_p = this.vportRt.ConvertWcsToScr(wcs_mp).ToSKPoint();
            var start = this.vportRt.ConvertWcsToScr(this.firstPoint).ToSKPoint();
            using (var elePen = new SKPaint { Color = SKColors.Yellow, IsStroke = true, PathEffect = SKPathEffect.CreateDash(new float[] { 10, 10, 30, 10 }, 0) })
            {
                canvas.DrawLine(start, sk_p, elePen);
            }
            Line2d baseline = new Line2d();
            baseline.Start = this.firstPoint;
            baseline.End = wcs_mp;
            foreach (var item in QdWall.GetShapeCurves(baseline, 200))
            {
                Line2d line2D = item as Line2d;
                //mstart = matrix.MultiplyPoint(line2D.Start);
                //mend = matrix.MultiplyPoint(line2D.End);
                start = this.vportRt.ConvertWcsToScr(line2D.Start).ToSKPoint();
                var end = this.vportRt.ConvertWcsToScr(line2D.End).ToSKPoint();
                using (var elePen = new SKPaint { Color = SKColors.Gray, IsStroke = true })
                {
                    canvas.DrawLine(start, end, elePen);
                }

            }

        }
        private bool CreateWall(Vector2 sp, Vector2 ep, double width = 200)
        {
            Line2d line2D = new Line2d();
            line2D.Start = sp;
            line2D.End = ep;
            var doc = this.docRt.Document;
            var wall = doc.CreateObject<QdWall>();
            wall.BaseCurve = line2D;
            // wall.Curves.Add(line2D);
            wall.Width = width;
            wall.Height = 3000;
            wall.Bottom = 0;
            wall.CreateShapeCurves();
            DisposeWalls(wall);

            this.docRt.Action.ClearSelects();
            return true;
        }
        private List<QdWall> GetCrossWalls(Line2d line2d)
        {
            var walls = this.docRt.Document.ModelSpace.Elements.Where((ele) => ele.Type == ArchElementType.Wall).ToList();
            List<QdWall> lwalls = new List<QdWall>();
            foreach (QdWall wall in walls)
            {
                if (line2d != wall.BaseLine)
                {
                    if (wall.BaseLine.Start.Similarity(line2d.Start) || wall.BaseLine.Start.Similarity(line2d.End) || wall.BaseLine.End.Similarity(line2d.Start) || wall.BaseLine.End.Similarity(line2d.End))
                    {
                        lwalls.Add(wall);
                    }
                    else
                        if (Line2d.CrossLine(line2d, wall.BaseLine, wall.Width/2))
                    {
                        lwalls.Add(wall);
                    }
                }
            }
            return lwalls;
        }
        private void DisposeWalls(QdWall wall, bool Create = true)
        {
            List<QdWall> Crosswalls = GetCrossWalls(wall.BaseLine);
            if (Crosswalls.Count > 0)
            {
                foreach (QdWall item in Crosswalls)
                {
                    if (item.BaseLine.Start.Similarity(wall.BaseLine.Start) || item.BaseLine.Start.Similarity(wall.BaseLine.End) || item.BaseLine.End.Similarity(wall.BaseLine.Start) || item.BaseLine.End.Similarity(wall.BaseLine.End))
                    {
                        CornerWalls(wall, item);
                        AssociateElement associateElement = new AssociateElement();
                        associateElement.Element = item;
                        associateElement.Name = "Wall";
                        associateElement.Tag = wall.Id;
                        associateElement.Type = AssociateType.Connect;
                        if (wall.AssociateElements.Where(x => x.Element == item).Count() == 0)
                        {
                            wall.AssociateElements.Add(associateElement);
                        }
                    }
                    else
                    if (Vector2.Distance(item.BaseLine.Start, wall.BaseLine.Start) < wall.Width / 2 || Vector2.Distance(item.BaseLine.Start, wall.BaseLine.End) < wall.Width / 2 || Vector2.Distance(item.BaseLine.End, wall.BaseLine.Start) < wall.Width / 2 || Vector2.Distance(item.BaseLine.End, wall.BaseLine.End) < wall.Width / 2)
                    {
                        if (Vector2.Distance(item.BaseLine.Start, wall.BaseLine.Start) < wall.Width / 2)
                        {
                            wall.BaseLine.Start = item.BaseLine.Start;
                        }
                        if (Vector2.Distance(item.BaseLine.End, wall.BaseLine.Start) < wall.Width / 2)
                        {
                            wall.BaseLine.Start = item.BaseLine.End;
                        }
                        if (Vector2.Distance(item.BaseLine.Start, wall.BaseLine.End) < wall.Width / 2)
                        {
                            wall.BaseLine.End = item.BaseLine.Start;

                        }
                        if (Vector2.Distance(item.BaseLine.End, wall.BaseLine.End) < wall.Width / 2)
                        {
                            wall.BaseLine.End = item.BaseLine.End;
                        }
                        wall.CreateShapeCurves();
                        CornerWalls(wall, item);
                        AssociateElement associateElement = new AssociateElement();
                        associateElement.Element = item;
                        associateElement.Name = "Wall";
                        associateElement.Tag = wall.Id;
                        associateElement.Type = AssociateType.Connect;
                        if (wall.AssociateElements.Where(x => x.Element == item).Count() == 0)
                        {
                            wall.AssociateElements.Add(associateElement);
                        }
                    }
                    else
                    {
                        Vector2 cp = Line2d.GetCrossVector(wall.BaseLine, item.BaseLine);
                        double sc = Vector2.Distance(cp, wall.BaseLine.Start);
                        double ec = Vector2.Distance(cp, wall.BaseLine.End);
                        double cl = sc < ec ? sc : ec;
                        double sc1 = Vector2.Distance(cp, item.BaseLine.Start);
                        double ec1 = Vector2.Distance(cp, item.BaseLine.End);
                        double cl1 = sc1 < ec1 ? sc1 : ec1;
                        if (cl < (item.Width / 2))//交点小于墙宽就算 转角墙
                        {
                            if (sc < ec)
                            {
                                ConnectWall(wall, item, wall.BaseLine.Start);
                            }
                            else
                            {
                                ConnectWall(wall, item, wall.BaseLine.End);

                            }
                        }
                        else if (cl1 < (wall.Width / 2))//交点小于墙宽就算 转角墙
                        {
                            if (sc1 < ec1)
                            {
                                ConnectWall(item, wall, item.BaseLine.Start);
                            }
                            else
                            {
                                ConnectWall(item, wall, item.BaseLine.End);

                            }
                        }
                        else//交叉墙 需要打断
                        {
                            CrossWall(wall, item);
                            AssociateElement associateElement = new AssociateElement();
                            associateElement.Element = item;
                     
                            associateElement.Name = "Wall";
                            associateElement.Tag = wall.Id;
                            associateElement.Type = AssociateType.Cross;
                            if (wall.AssociateElements.Where(x => x.Element == item).Count() == 0)
                            {
                                wall.AssociateElements.Add(associateElement);
                            }
                        }
                    }
                }
                if (Create)
                    this.docRt.Document.ModelSpace.InsertElement(wall);
            }
            else
            {
                if (Create)
                    this.docRt.Document.ModelSpace.InsertElement(wall);
            }
        }

        private void changepoint(List<Curve2d> nc, List<Curve2d> oc, Vector2 jp, Vector2 yp, List<Vector2> nps, List<Vector2> ops, Vector2 np, Vector2 op, double n, double o)
        {
            int nj = 0;
            int ny = 0;
            int oj = 0;
            int oy = 0;
            if (Vector2.Distance(nps[0], np) < Vector2.Distance(nps[1], np))
            {
                if (n == 1)
                {
                    nj = 0;
                }
                else
                {
                    ny = 0;
                }
            }
            else
            {
                if (n == 1)
                {
                    nj = 1;
                }
                else
                {
                    ny = 1;
                }
            }
            if (Vector2.Distance(nps[2], np) < Vector2.Distance(nps[3], np))
            {
                if (n == 1)
                {
                    ny = 2;

                }
                else
                {
                    nj = 2;
                }

            }
            else
            {

                if (n == 1)
                {
                    ny = 3;

                }
                else
                {
                    nj = 3;
                }
            }

            if (Vector2.Distance(ops[0], op) < Vector2.Distance(ops[1], op))
            {
                if (o == 1)
                {
                    oj = 0;
                }
                else
                {
                    oy = 0;
                }
            }
            else
            {
                if (o == 1)
                {
                    oj = 1;
                }
                else
                {
                    oy = 1;
                }
            }
            if (Vector2.Distance(ops[2], op) < Vector2.Distance(ops[3], op))
            {
                if (o == 1)
                {
                    oy = 2;

                }
                else
                {
                    oj = 2;
                }

            }
            else
            {

                if (o == 1)
                {
                    oy = 3;

                }
                else
                {
                    oj = 3;
                }
            }

            foreach (Line2d item in oc)
            {
                if (item.Start.Similarity(ops[oj]) || item.End.Similarity(ops[oj]))
                {
                    if (item.Start.Similarity(ops[oj]))
                        item.Start = jp;
                    else
                        item.End = jp;
                }
                if (item.Start.Similarity(ops[oy]) || item.End.Similarity(ops[oy]))
                {
                    if (item.Start.Similarity(ops[oy]))
                        item.Start = yp;
                    else
                        item.End = yp;
                }
            }

            foreach (Line2d item in nc)
            {
                if (item.Start.Similarity(nps[nj]) || item.End.Similarity(nps[nj]))
                {
                    if (item.Start.Similarity(nps[nj]))
                        item.Start = jp;
                    else
                        item.End = jp;
                }
                if (item.Start.Similarity(nps[ny]) || item.End.Similarity(nps[ny]))
                {
                    if (item.Start.Similarity(nps[ny]))
                        item.Start = yp;
                    else
                        item.End = yp;
                }
            }
            nps[nj] = jp;
            ops[oj] = jp;
            nps[ny] = yp;
            ops[oy] = yp;
        }
        private void removelinebyPoint(List<Curve2d> nc, List<Curve2d> oc, Vector2 np, Vector2 op)
        {
            List<Line2d> reline2d = new List<Line2d>();
            reline2d = new List<Line2d>();
            foreach (Line2d item in nc)
            {
                if (Line2d.PointInLine(item, np))
                {
                    reline2d.Add(item);
                    break;
                }
            }
            foreach (var item in reline2d)
                nc.Remove(item);
            reline2d = new List<Line2d>();
            foreach (Line2d item in oc)
            {
                if (Line2d.PointInLine(item, op))
                {
                    reline2d.Add(item);
                    break;
                }
            }
            foreach (var item in reline2d)
                oc.Remove(item);
        }
        private void CornerWalls(QdWall nwall, QdWall owall)
        {
            List<Line2d> reline2d = new List<Line2d>();

            Line2d nline1 = new Line2d { Start = nwall.Points[0], End = nwall.Points[1] };
            Line2d nline2 = new Line2d { Start = nwall.Points[2], End = nwall.Points[3] };
            Line2d oline1 = new Line2d { Start = owall.Points[0], End = owall.Points[1] };
            Line2d oline2 = new Line2d { Start = owall.Points[2], End = owall.Points[3] };
            if (nwall.BaseLine.Start.Similarity(owall.BaseLine.Start))
            {
                removelinebyPoint(nwall.Curves, owall.Curves, nwall.BaseLine.Start, owall.BaseLine.Start);


                if (Line2d.CrossLine(nline1, oline1))
                {
                    if (!Line2d.CrossLine(nline1, oline2))
                    {
                        Vector2 jp = Line2d.GetCrossVector(nline1, oline1);
                        Vector2 yp = Line2d.GetCrossVector(nline2, oline2);
                        changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.Start, 1, 1);

                    }
                    else
                    {
                        Vector2 p2 = Line2d.GetCrossVector(nline1, oline2);
                        Vector2 p1 = Line2d.GetCrossVector(nline1, oline1);
                        if (Vector2.Distance(p1, nwall.BaseLine.Start) > Vector2.Distance(p2, nwall.BaseLine.Start))
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline1, oline1);
                            Vector2 yp = Line2d.GetCrossVector(nline2, oline2);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.Start, 1, 1);
                        }
                        else
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline1, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline2, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.Start, 1, 2);
                        }

                    }
                }
                else if (Line2d.CrossLine(nline1, oline2))
                {

                    if (!Line2d.CrossLine(nline2, oline2))
                    {
                        Vector2 jp = Line2d.GetCrossVector(nline1, oline2);
                        Vector2 yp = Line2d.GetCrossVector(nline2, oline1);
                        changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.Start, 1, 2);
                    }
                    else
                    {
                        Vector2 p1 = Line2d.GetCrossVector(nline1, oline2);
                        Vector2 p2 = Line2d.GetCrossVector(nline2, oline2);
                        if (Vector2.Distance(p1, nwall.BaseLine.Start) > Vector2.Distance(p2, nwall.BaseLine.Start))
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline1, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline2, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.Start, 1, 2);
                        }
                        else
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline2, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline1, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.Start, 2, 2);
                        }
                    }

                }
                else if (Line2d.CrossLine(nline2, oline1))
                {
                    if (!Line2d.CrossLine(nline2, oline2))
                    {
                        Vector2 jp = Line2d.GetCrossVector(nline2, oline1);
                        Vector2 yp = Line2d.GetCrossVector(nline1, oline2);
                        changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.Start, 2, 1);
                    }
                    else
                    {
                        Vector2 p1 = Line2d.GetCrossVector(nline2, oline1);
                        Vector2 p2 = Line2d.GetCrossVector(nline2, oline2);
                        if (Vector2.Distance(p1, nwall.BaseLine.Start) > Vector2.Distance(p2, nwall.BaseLine.Start))
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline2, oline1);
                            Vector2 yp = Line2d.GetCrossVector(nline1, oline2);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.Start, 2, 1);
                        }
                        else
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline2, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline1, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.Start, 2, 2);
                        }
                    }
                }
                else if (Line2d.CrossLine(nline2, oline2))
                {
                    Vector2 jp = Line2d.GetCrossVector(nline2, oline2);
                    Vector2 yp = Line2d.GetCrossVector(nline1, oline1);
                    changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.Start, 2, 2);
                }
            }
            else if (nwall.BaseLine.Start.Similarity(owall.BaseLine.End))
            {
                removelinebyPoint(nwall.Curves, owall.Curves, nwall.BaseLine.Start, owall.BaseLine.End);


                if (Line2d.CrossLine(nline1, oline1))
                {

                    if (!Line2d.CrossLine(nline1, oline2))
                    {
                        Vector2 jp = Line2d.GetCrossVector(nline1, oline1);
                        Vector2 yp = Line2d.GetCrossVector(nline2, oline2);
                        changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.End, 1, 1);

                    }
                    else
                    {
                        Vector2 p2 = Line2d.GetCrossVector(nline1, oline2);
                        Vector2 p1 = Line2d.GetCrossVector(nline1, oline1);
                        if (Vector2.Distance(p1, nwall.BaseLine.Start) > Vector2.Distance(p2, nwall.BaseLine.Start))
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline1, oline1);
                            Vector2 yp = Line2d.GetCrossVector(nline2, oline2);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.End, 1, 1);
                        }
                        else
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline1, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline2, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.End, 1, 2);
                        }

                    }

                }
                else if (Line2d.CrossLine(nline1, oline2))
                {
                    if (!Line2d.CrossLine(nline2, oline2))
                    {
                        Vector2 jp = Line2d.GetCrossVector(nline1, oline2);
                        Vector2 yp = Line2d.GetCrossVector(nline2, oline1);
                        changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.End, 1, 2);
                    }
                    else
                    {
                        Vector2 p1 = Line2d.GetCrossVector(nline1, oline2);
                        Vector2 p2 = Line2d.GetCrossVector(nline2, oline2);
                        if (Vector2.Distance(p1, nwall.BaseLine.Start) > Vector2.Distance(p2, nwall.BaseLine.Start))
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline1, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline2, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.End, 1, 2);
                        }
                        else
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline2, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline1, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.End, 2, 2);
                        }
                    }

                }
                else if (Line2d.CrossLine(nline2, oline1))
                {
                    if (!Line2d.CrossLine(nline2, oline2))
                    {
                        Vector2 jp = Line2d.GetCrossVector(nline2, oline1);
                        Vector2 yp = Line2d.GetCrossVector(nline1, oline2);
                        changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.End, 2, 1);
                    }
                    else
                    {
                        Vector2 p1 = Line2d.GetCrossVector(nline2, oline1);
                        Vector2 p2 = Line2d.GetCrossVector(nline2, oline2);
                        if (Vector2.Distance(p1, nwall.BaseLine.Start) > Vector2.Distance(p2, nwall.BaseLine.Start))
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline2, oline1);
                            Vector2 yp = Line2d.GetCrossVector(nline1, oline2);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.End, 2, 1);
                        }
                        else
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline2, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline1, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.End, 2, 2);
                        }
                    }
                }
                else if (Line2d.CrossLine(nline2, oline2))
                {
                    Vector2 jp = Line2d.GetCrossVector(nline2, oline2);
                    Vector2 yp = Line2d.GetCrossVector(nline1, oline1);
                    changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.Start, owall.BaseLine.End, 2, 2);
                }
            }
            else if (nwall.BaseLine.End.Similarity(owall.BaseLine.Start))
            {
                removelinebyPoint(nwall.Curves, owall.Curves, nwall.BaseLine.End, owall.BaseLine.Start);


                if (Line2d.CrossLine(nline1, oline1))
                {

                    if (!Line2d.CrossLine(nline1, oline2))
                    {
                        Vector2 jp = Line2d.GetCrossVector(nline1, oline1);
                        Vector2 yp = Line2d.GetCrossVector(nline2, oline2);
                        changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.Start, 1, 1);

                    }
                    else
                    {
                        Vector2 p2 = Line2d.GetCrossVector(nline1, oline2);
                        Vector2 p1 = Line2d.GetCrossVector(nline1, oline1);
                        if (Vector2.Distance(p1, nwall.BaseLine.End) > Vector2.Distance(p2, nwall.BaseLine.End))
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline1, oline1);
                            Vector2 yp = Line2d.GetCrossVector(nline2, oline2);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.Start, 1, 1);
                        }
                        else
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline1, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline2, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.Start, 1, 2);
                        }

                    }

                }
                else if (Line2d.CrossLine(nline1, oline2))
                {


                    if (!Line2d.CrossLine(nline2, oline2))
                    {
                        Vector2 jp = Line2d.GetCrossVector(nline1, oline2);
                        Vector2 yp = Line2d.GetCrossVector(nline2, oline1);
                        changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.Start, 1, 2);
                    }
                    else
                    {
                        Vector2 p1 = Line2d.GetCrossVector(nline1, oline2);
                        Vector2 p2 = Line2d.GetCrossVector(nline2, oline2);
                        if (Vector2.Distance(p1, nwall.BaseLine.End) > Vector2.Distance(p2, nwall.BaseLine.End))
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline1, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline2, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.Start, 1, 2);
                        }
                        else
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline2, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline1, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.Start, 2, 2);
                        }
                    }

                }
                else if (Line2d.CrossLine(nline2, oline1))
                {

                    if (!Line2d.CrossLine(nline2, oline2))
                    {
                        Vector2 jp = Line2d.GetCrossVector(nline2, oline1);
                        Vector2 yp = Line2d.GetCrossVector(nline1, oline2);
                        changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.Start, 2, 1);
                    }
                    else
                    {
                        Vector2 p1 = Line2d.GetCrossVector(nline2, oline1);
                        Vector2 p2 = Line2d.GetCrossVector(nline2, oline2);
                        if (Vector2.Distance(p1, nwall.BaseLine.End) > Vector2.Distance(p2, nwall.BaseLine.End))
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline2, oline1);
                            Vector2 yp = Line2d.GetCrossVector(nline1, oline2);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.Start, 2, 1);
                        }
                        else
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline2, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline1, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.Start, 2, 2);
                        }
                    }
                }
                else if (Line2d.CrossLine(nline2, oline2))
                {
                    Vector2 jp = Line2d.GetCrossVector(nline2, oline2);
                    Vector2 yp = Line2d.GetCrossVector(nline1, oline1);
                    changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.Start, 2, 2);
                }
            }
            else
            {
                removelinebyPoint(nwall.Curves, owall.Curves, nwall.BaseLine.End, owall.BaseLine.End);


                if (Line2d.CrossLine(nline1, oline1))
                {

                    if (!Line2d.CrossLine(nline1, oline2))
                    {
                        Vector2 jp = Line2d.GetCrossVector(nline1, oline1);
                        Vector2 yp = Line2d.GetCrossVector(nline2, oline2);
                        changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.End, 1, 1);

                    }
                    else
                    {
                        Vector2 p2 = Line2d.GetCrossVector(nline1, oline2);
                        Vector2 p1 = Line2d.GetCrossVector(nline1, oline1);
                        if (Vector2.Distance(p1, nwall.BaseLine.End) > Vector2.Distance(p2, nwall.BaseLine.End))
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline1, oline1);
                            Vector2 yp = Line2d.GetCrossVector(nline2, oline2);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.End, 1, 1);
                        }
                        else
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline1, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline2, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.End, 1, 2);
                        }

                    }


                }
                else if (Line2d.CrossLine(nline1, oline2))
                {

                    if (!Line2d.CrossLine(nline2, oline2))
                    {
                        Vector2 jp = Line2d.GetCrossVector(nline1, oline2);
                        Vector2 yp = Line2d.GetCrossVector(nline2, oline1);
                        changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.End, 1, 2);
                    }
                    else
                    {
                        Vector2 p1 = Line2d.GetCrossVector(nline1, oline2);
                        Vector2 p2 = Line2d.GetCrossVector(nline2, oline2);
                        if (Vector2.Distance(p1, nwall.BaseLine.End) > Vector2.Distance(p2, nwall.BaseLine.End))
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline1, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline2, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.End, 1, 2);
                        }
                        else
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline2, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline1, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.End, 2, 2);
                        }
                    }

                }
                else if (Line2d.CrossLine(nline2, oline1))
                {


                    if (!Line2d.CrossLine(nline2, oline2))
                    {
                        Vector2 jp = Line2d.GetCrossVector(nline2, oline1);
                        Vector2 yp = Line2d.GetCrossVector(nline1, oline2);
                        changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.End, 2, 1);
                    }
                    else
                    {
                        Vector2 p1 = Line2d.GetCrossVector(nline2, oline1);
                        Vector2 p2 = Line2d.GetCrossVector(nline2, oline2);
                        if (Vector2.Distance(p1, nwall.BaseLine.End) > Vector2.Distance(p2, nwall.BaseLine.End))
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline2, oline1);
                            Vector2 yp = Line2d.GetCrossVector(nline1, oline2);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.End, 2, 1);
                        }
                        else
                        {
                            Vector2 jp = Line2d.GetCrossVector(nline2, oline2);
                            Vector2 yp = Line2d.GetCrossVector(nline1, oline1);
                            changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.End, 2, 2);
                        }
                    }
                }
                else if (Line2d.CrossLine(nline2, oline2))
                {
                    Vector2 jp = Line2d.GetCrossVector(nline2, oline2);
                    Vector2 yp = Line2d.GetCrossVector(nline1, oline1);
                    changepoint(nwall.Curves, owall.Curves, jp, yp, nwall.Points, owall.Points, nwall.BaseLine.End, owall.BaseLine.End, 2, 2);
                }
            }
        }

        private void AdheringWall(QdWall nwall, QdWall owall, Vector2 cp)
        {
            double sc = Vector2.Distance(cp, nwall.BaseLine.Start);
            double ec = Vector2.Distance(cp, nwall.BaseLine.End);
            if (sc < ec)
            {
            }
            else
            {

            }
        }
        private void CrossWall(QdWall nwall, QdWall owall)
        {

            Line2d nline1 = new Line2d { Start = nwall.Points[0], End = nwall.Points[1] };
            Line2d nline2 = new Line2d { Start = nwall.Points[2], End = nwall.Points[3] };
            Line2d oline1 = new Line2d { Start = owall.Points[0], End = owall.Points[1] };
            Line2d oline2 = new Line2d { Start = owall.Points[2], End = owall.Points[3] };
            Dictionary<Line2d, List<Line2d>> dls = new Dictionary<Line2d, List<Line2d>>();
            foreach (Line2d item in nwall.Curves)
            {
                if (Line2d.CrossLine(item, oline1) && Line2d.CrossLine(item, oline2))
                {
                    dls.Add(item, Line2d.BreakLineBy2line(item, oline1, oline2));

                }
            }
            foreach (var item in dls)
            {
                nwall.Curves.Remove(item.Key);
                nwall.Curves.AddRange(item.Value);
            }
            dls = new Dictionary<Line2d, List<Line2d>>();
            foreach (Line2d item in owall.Curves)
            {
                if (Line2d.CrossLine(item, nline1) && Line2d.CrossLine(item, nline2))
                {
                    dls.Add(item, Line2d.BreakLineBy2line(item, nline1, nline2));

                }
            }
            foreach (var item in dls)
            {
                owall.Curves.Remove(item.Key); owall.Curves.AddRange(item.Value);
            }
        }
        private void ConnectWall(QdWall nwall, QdWall owall, Vector2 p)
        {
            Line2d nline1 = new Line2d { Start = nwall.Points[0], End = nwall.Points[1] };
            Line2d nline2 = new Line2d { Start = nwall.Points[2], End = nwall.Points[3] };
            Line2d oline1 = new Line2d { Start = owall.Points[0], End = owall.Points[1] };
            Line2d oline2 = new Line2d { Start = owall.Points[2], End = owall.Points[3] };
            List<Line2d> removes = new List<Line2d>();
            foreach (Line2d item in nwall.Curves)
            {
                if (Line2d.PointInLine(item, p))
                {
                    removes.Add(item);
                }

            }
            foreach (Line2d item in removes)
                nwall.Curves.Remove(item);



            Dictionary<Line2d, List<Line2d>> dls = new Dictionary<Line2d, List<Line2d>>();
            foreach (Line2d item in owall.Curves)
            {
                if (Line2d.CrossLine(item, nline1) && Line2d.CrossLine(item, nline2))
                {
                    dls.Add(item, Line2d.BreakLineBy2line(item, nline1, nline2));

                }
            }
            foreach (var item in dls)
            {
                owall.Curves.Remove(item.Key); owall.Curves.AddRange(item.Value);
            }


            foreach (Line2d item in nwall.Curves)
            {
                if (Line2d.CrossLine(item, oline1))
                {
                    Vector2 cp = Line2d.GetCrossVector(item, oline1);
                    double sc = Vector2.Distance(cp, item.Start);
                    double ec = Vector2.Distance(cp, item.End);
                    if (sc < ec)
                    {
                        item.Start = cp;
                    }
                    else
                    {
                        item.End = cp;
                    }

                }
                if (Line2d.CrossLine(item, oline2))
                {
                    Vector2 cp = Line2d.GetCrossVector(item, oline2);
                    double sc = Vector2.Distance(cp, item.Start);
                    double ec = Vector2.Distance(cp, item.End);
                    if (sc < ec)
                    {
                        item.Start = cp;
                    }
                    else
                    {
                        item.End = cp;
                    }
                }
            }

            if (Line2d.CrossLine(nline1, oline1))
            {
                Vector2 cp = Line2d.GetCrossVector(nline1, oline1);
                double sc = Vector2.Distance(cp, nline1.Start);
                double ec = Vector2.Distance(cp, nline1.End);
                if (sc < ec)
                {
                    nwall.Points[0] = cp;
                }
                else
                {
                    nwall.Points[1] = cp;
                }
            }
            else if (Line2d.CrossLine(nline1, oline2))
            {
                Vector2 cp = Line2d.GetCrossVector(nline1, oline2);
                double sc = Vector2.Distance(cp, nline1.Start);
                double ec = Vector2.Distance(cp, nline1.End);
                if (sc < ec)
                {
                    nwall.Points[0] = cp;
                }
                else
                {
                    nwall.Points[1] = cp;
                }
            }
            if (Line2d.CrossLine(nline2, oline1))
            {
                Vector2 cp = Line2d.GetCrossVector(nline2, oline1);
                double sc = Vector2.Distance(cp, nline2.Start);
                double ec = Vector2.Distance(cp, nline2.End);
                if (sc < ec)
                {
                    nwall.Points[2] = cp;
                }
                else
                {
                    nwall.Points[3] = cp;
                }
            }
            else if (Line2d.CrossLine(nline2, oline2))
            {
                Vector2 cp = Line2d.GetCrossVector(nline2, oline2);
                double sc = Vector2.Distance(cp, nline2.Start);
                double ec = Vector2.Distance(cp, nline2.End);
                if (sc < ec)
                {
                    nwall.Points[2] = cp;
                }
                else
                {
                    nwall.Points[3] = cp;
                }
            }
            if (Line2d.CrossLine(nwall.BaseLine, oline1))
            {
                Vector2 cp = Line2d.GetCrossVector(nwall.BaseLine, oline1);
                double sc = Vector2.Distance(cp, nwall.BaseLine.Start);
                double ec = Vector2.Distance(cp, nwall.BaseLine.End);
                if (sc < ec)
                {
                    Line2d basel = new Line2d();
                    basel.Start = cp;
                    basel.End = nwall.BaseLine.End;
                    nwall.BaseCurve = basel;

                }
                else
                {
                    Line2d basel = new Line2d();
                    basel.Start = nwall.BaseLine.Start;
                    basel.End = cp;
                    nwall.BaseCurve = basel;

                }
            }
            else if (Line2d.CrossLine(nwall.BaseLine, oline2))
            {
                Vector2 cp = Line2d.GetCrossVector(nwall.BaseLine, oline2);
                double sc = Vector2.Distance(cp, nwall.BaseLine.Start);
                double ec = Vector2.Distance(cp, nwall.BaseLine.End);
                if (sc < ec)
                {
                    Line2d basel = new Line2d();
                    basel.Start = cp;
                    basel.End = nwall.BaseLine.End;
                    nwall.BaseCurve = basel;

                }
                else
                {
                    Line2d basel = new Line2d();
                    basel.Start = nwall.BaseLine.Start;
                    basel.End = cp;
                    nwall.BaseCurve = basel;

                }
            }
        }
        public override SnapPointResult SnapPoint(SnapRuntime snapRt, LcElement element, Vector2 point, bool forRef)
        {

            var maxDistance = vportRt.GetSnapMaxDistance();
            QdWall wall = element as QdWall;
            var sscur = SnapSettings.Current;
            var result = new SnapPointResult { Element = element };
            var line2d = wall.BaseCurve;
            var line = wall.BaseLine;
            if (sscur.ObjectOn)
            {

                if (sscur.PointType.Has(SnapPointType.Endpoint))
                {
                    if ((point - line.Start).Length() <= maxDistance)
                    {
                        result.Point = line.Start;
                        result.Name = "Start";
                        result.Curves.Add(new SnapRefCurve(SnapPointType.Endpoint, line2d));
                    }
                    else if ((point - line.End).Length() <= maxDistance)
                    {
                        result.Point = line.End;
                        result.Name = "End";
                        result.Curves.Add(new SnapRefCurve(SnapPointType.Endpoint, line2d));
                    }
                }
                if (sscur.PointType.Has(SnapPointType.Midpoint) && result.Point == null)
                {
                    var cp = (line.Start + line.End) / 2;
                    if ((point - cp).Length() <= maxDistance)
                    {
                        result.Point = cp;
                        result.Name = "Mid";
                        result.Curves.Add(new SnapRefCurve(SnapPointType.Midpoint, line2d));
                    }
                }
                //forRef时只捕捉端点，中点等，如果
                if (!forRef && result.Point == null)
                {
                    var refPoints = snapRt.GetRefPoints(element);
                    var lineLength = (line.End - line.Start).Length();
                    var distance = GeoUtils.PointToLineDistance(line.Start, line.End, point, out Vector2 nearest, out double dirDistance);
                    if (sscur.PointType.Has(SnapPointType.Nearest))
                    {
                        //最近点必须在线段内进行测试
                        if (distance < maxDistance && dirDistance > 0 && dirDistance < lineLength)
                        {
                            result.Point = nearest;
                            result.Name = "Nearest";
                            result.Curves.Add(new SnapRefCurve(SnapPointType.Nearest, line2d));
                        }
                    }
                    //进行延长线上点捕捉，需要元素上有参考点
                    if (sscur.PointType.Has(SnapPointType.ExtensionLine) && refPoints.Count > 0 && result.Point == null)
                    {
                        if (distance < maxDistance)
                        {
                            result.Point = nearest;
                            result.Name = "Extension";
                            result.Curves.Add(new SnapRefCurve(SnapPointType.ExtensionLine, line2d));
                        }
                    }
                }
            }

            if (result.Point != null)
                return result;
            else
                return null;
        }
    }
}
